$(function () {
    setTimeout(hookHash, 500);
    hookHeaderNav();
    animateLogo();
    hookImageHover();
});

function hookHash() {
    isElementScroll(window.location.hash, null);
    setTimeout(hookSliderNav, 500);

    $("#headerNav a[href*=#goto],#footerNav a[href*=#goto]").click(function () {
        var the = $(this);
        return isElementScroll(the.attr("href"), the);
    });

    function isElementScroll(href, obj) {
        var pos = href.indexOf("#goto");
        if (pos > -1) {
            var ele = href.substr(pos).replace("#goto", "#");
            if (ele.length > 1) {
                var e = $(ele);
                if (e.size() > 0) {
                    var top = Math.max(e.position().top - 81, 0);
                    scrollPage(top);

                    if (obj == null) {
                        obj = $("#headerNav a[href*='" + href + "']");
                    }
                    if (obj.size() > 0) {
                        $("#headerNav .current").removeClass("current");
                        if (obj.hasClass("items")) {
                            obj.addClass("current");
                        }
                        else {
                            var items = obj.parent().parent().parent().find(".items");
                            if (items.size() > 0) {
                                items.addClass("current");
                            }
                        }
                    }
                    location.hash = href.substr(pos);
                    return false;
                }
            }
        }
        return true;
    } 
}
function scrollPage(top, cb) {
    $("html,body").stop(true, true).animate({ "scrollTop": top }, "slow", function () {
        cb && cb();
    });
}

function hookHeaderNav() {
    var _timeOut = null;
    var _taggoleElement = null;
    $("#headerNav li:has(ul)").hover(function () {
        if (_timeOut) {
            clearTimeout(_timeOut);
        }

        var the = $(this);
        if (_taggoleElement) {
            var a = _taggoleElement.find(".items").attr("href");
            var t = the.find(".items").attr("href");
            if (a != t) {
                hideChild();
            }
            else {
                return;
            }
        }

        the.find(".items").addClass("sub hover");
        var ul = the.find("ul");
        setNavWidth(ul, the.position().left + the.width() + 60);
        $("#headerNavBg").stop(true, true).slideDown("slow");
        ul.delay(100).slideDown("slow");
    }, function () {
        _taggoleElement = $(this);
        _timeOut = setTimeout(hideChild, 1000);
    });

    function hideChild() {
        if (_taggoleElement) {
            var a = _taggoleElement.find(".items");
            a.removeClass("sub hover");
            _taggoleElement.find("ul").stop(true, true).slideUp();
            $("#headerNavBg").delay(100).slideUp();
            _taggoleElement = null;
        }
    }

    function setNavWidth(the, left) {
        if (!the.attr("W")) {
            var w = 4;
            the.find("li").each(function () {
                w += $(this).width();
            });
            the.attr("W", true);
            var l = Math.max((left) - (w / 2), 0);
            the.css({ "width": w, "left": l, "top": "60px", "display": "none" });
        }
    }
}

function animateLogo() {
    var logo2 = new Image;
    logo2.src = "logo2.png";
    $("#headerLogo").hover(function () {
        RunLogoRotate($(this));
    }, function () {
        //RunLogoRotate($(this));
    });

    var _isRunLogoRotate = false;
    function RunLogoRotate(the) {
        if (_isRunLogoRotate)
            return;

        _isRunLogoRotate = true;
        var img = the.find("img");
        var imgSrc = img.data("src");
        var src = img.attr("src");

        the.css("background-image", "none");
        img.stop(true,true).animate({ "width": 0, "height": 114, "left": 57, "top": 0 }, 400, function () {
            img.attr("src", imgSrc);
            img.stop(true, true).animate({ "width": 114, "height": 114, "left": 0, "top": 0 }, 400, function () {
                the.css("background-image", "url(logoMaskBg.png)");
                _isRunLogoRotate = false;
                img.data("src", src);
            });
        });
    }
}

function hookImageHover() {
    $("img[hover]").each(function () {
        var m = new Image;
        m.src = $(this).attr("hover");
    }).hover(hoverImageSrc, hoverImageSrc);

    function hoverImageSrc() {
        var the = $(this);
        var src = the.attr("src");
        the.attr("src", the.attr("hover"));
        the.attr("hover", src);
    }
}

function hookSliderNav() {
    var obj = $("#SliderNav");
    if (obj.size() == 0)
        return;

    obj.find("li").each(function () {
        var the = $(this);
        var a = the.find("a");
        var href = a.attr("href");
        var pos = href.indexOf("#goto");
        if (pos > -1) {
            var ele = href.substr(pos).replace("#goto", "#");
            if (ele.length > 0) {
                var e = $(ele);
                if (e.size() > 0) {
                    var top = Math.max(e.position().top - 81, 0);
                    a.data("top", top);
                    a.data("hash", href.substr(pos));
                    a.click(function () {
                        scrollPage($(this).data("top"));
                        location.hash = $(this).data("hash");
                        return false;
                    });
                }
            }
        }
    });
}